package cn.com.infcn.spider.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import cn.com.infcn.spider.domain.Page;

/**
 * 提供了网页下载的工具包
 * 
 * @author tjx
 * 
 */
public class HttpUtils {

	private static final Logger LOG = Logger.getLogger(HttpUtils.class);

	/**
	 * 下载一个网页的内容去除html
	 * 
	 * @param url
	 * @return
	 */
	public static String getText(String url) {
		Document document = getDocument(url);
		if (document != null) {
			return document.text();
		}
		return null;
	}

	/**
	 * 返回jsonp的文档
	 * 
	 * @param url
	 * @return
	 */
	public static Document getDocument(String url) {
		return getDocument(url, 3000);
	}

	/**
	 * 返回jsonp的文档
	 * 
	 * @param url
	 * @return
	 */
	public static Document getDocument(String url, int timeout) {
		Document document = null;
		try {
			document = Jsoup.connect(url).userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)").referrer("www.test.com").timeout(timeout).get();
			return document;
		} catch (Exception e) {
			LOG.error(url + " gather err !" + e.getMessage());
			throw new RuntimeException(e);
		}
	}

	public static Page getPage(String url, int timeout) {
		Page page = new Page();
		page.setUrl(url);
		try {
			Document document = getDocument(url, timeout);
			page.setHtml(document.html());
			page.setDocument(document);
			page.setOk(true);
		} catch (Exception e) {
			page.setOk(false);
			page.setMessage(e.getMessage());
		}

		return page;
	}

	/**
	 * 运行js获得页面
	 * 
	 * @param url
	 * @param timeout
	 * @param wait
	 * @return
	 */
	public static Page getPageByJs(String url) {
		Page page = new Page();
		Process p = null;
		try {
			Runtime rt = Runtime.getRuntime();
			p = rt.exec("phantomjs conf/ajax.js " + url);// 这里我的codes.js是保存在c盘下面的phantomjs目录
			StringBuilder sb = new StringBuilder();
			try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
				String tmp = null;
				while ((tmp = br.readLine()) != null) {
					sb.append(tmp);
				}
			}
			String html = sb.toString();

			Document document = Jsoup.parse(html, url);
			page.setHtml(document.html());
			page.setDocument(document);
			page.setOk(true);

		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			page.setOk(false);
			page.setMessage(e.getMessage());
		} finally {
			if (p != null) {
				p.destroy();
			}
		}

		return page;
	}

	public static void main(String[] args) {
		Page page = getPageByJs("http://baike.baidu.com/link?url=s-fhDiDj6iHgu2NO2J9TVGVIxiVg-bMWD1DyMLqLyYiiS3IZ3wZ-L0aH_5-56gJrK-bCMEtlQtLeTqIMBDE4qq");

		System.out.println(page.getDocument().getElementById("rs-7318094-5565").text());
	}

	/**
	 * 下载一个网页的内容包含html
	 * 
	 * @param url
	 * @return
	 */
	public static String getHtml(String url) {
		Document document = getDocument(url);
		if (document != null) {
			return document.html();
		}
		return null;
	}

	/**
	 * 下载一个网页的内容包含html
	 * 
	 * @param url
	 * @return
	 */
	public static Document postDocument(String url, Map<String, String> data) {
		Document document = null;
		try {
			document = Jsoup.connect(url).userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)").referrer("www.test.com").data(data).post();
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error(e);
		}
		return document;
	}

	/**
	 * 下载一个网页的内容包含html
	 * 
	 * @param url
	 * @return
	 */
	public static String postHtml(String url, Map<String, String> data) {
		Document document = postDocument(url, data);
		if (document != null) {
			return document.html();
		}
		return null;
	}

	/**
	 * 通过get方式获取json数据
	 * 
	 * @param url
	 * @return
	 */
	public static String getJSONStr(String url) {
		try {
			Response response = Jsoup.connect(url).timeout(600000).execute();
			return response.body();
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error(e);
		}
		return null;

	}

	/**
	 * 通过get方式获取json数据
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static String getJSONStr(String url, int timeout) throws IOException {
		Response response = Jsoup.connect(url).timeout(timeout).execute();
		return response.body();

	}

	/**
	 * 通过post方式获取json数据
	 * 
	 * @param url
	 * @return
	 */
	public static String postJSONStr(String url, Map<String, String> data) {
		try {

			Connection conn = Jsoup.connect(url).timeout(600000).method(Method.POST);
			if (data != null && !data.isEmpty()) {
				conn.data(data);
			}
			Response response = conn.execute();
			return response.body();
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error(e);
		}
		return null;

	}

}
